package com.parking.mgr.useraccount.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parking.mgr.common.constant.CommonConstant;
import com.parking.mgr.common.enums.CommonStatusEnum;
import com.parking.mgr.common.exception.ServiceException;
import com.parking.mgr.common.exception.enums.CommonExceptionEnum;
import com.parking.mgr.common.exception.enums.DeleteUpdateExceptionEnums;
import com.parking.mgr.common.factory.PageFactory;
import com.parking.mgr.common.util.IdUtils;
import com.parking.mgr.useraccount.facede.BasicRoleServiceFacede;
import com.parking.mgr.useraccount.model.entity.BasicRoleAssignEntity;
import com.parking.mgr.useraccount.model.entity.BasicRoleEntity;
import com.parking.mgr.useraccount.model.mapper.BasicRoleAssignMapper;
import com.parking.mgr.useraccount.model.mapper.BasicRoleMapper;
import com.parking.mgr.useraccount.model.mapstruct.BasicRoleAssignMapStruct;
import com.parking.mgr.useraccount.model.param.BasicRoleAssignParam;
import com.parking.mgr.useraccount.model.vo.BasicRoleAssignVO;
import com.parking.mgr.useraccount.service.BasicRoleAssignService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @ClassName BasicRoleAssignServiceImpl
 * @Description TODO
 * @Auther: shilintao
 * @Date: 2023/5/30
 */
@Service
public class BasicRoleAssignServiceImpl extends ServiceImpl<BasicRoleAssignMapper, BasicRoleAssignEntity> implements BasicRoleAssignService {

    @Resource
    private BasicRoleAssignMapper basicRoleAssignMapper;

    @Resource
    private BasicRoleMapper basicRoleMapper;

    @Resource
    private BasicRoleServiceFacede basicRoleServiceFacede;


    @Resource
    private BasicRoleAssignMapStruct basicRoleAssignMapStruct;

    @Override
    public IPage<BasicRoleAssignVO> list(BasicRoleAssignParam basicRoleAssignParam) {
        LambdaQueryWrapper<BasicRoleAssignEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotNull(basicRoleAssignParam)) {
            // 根据id进行查询
            if (ObjectUtil.isNotNull(basicRoleAssignParam.getId())) {
                queryWrapper.eq(BasicRoleAssignEntity::getId, basicRoleAssignParam.getId());
            }
            if (ObjectUtil.isNotNull(basicRoleAssignParam.getFkBasicRoleId())) {
                queryWrapper.eq(BasicRoleAssignEntity::getFkRoleAssignId, basicRoleAssignParam.getFkBasicRoleId());
            }

        }
        // 根据插入时间进行升序排序
        queryWrapper.orderByAsc(BasicRoleAssignEntity::getFInsertTimeMillis);

        Page<BasicRoleAssignEntity> page = PageFactory.defaultPage(basicRoleAssignParam.getPageSize(), basicRoleAssignParam.getCurrPage());
        IPage<BasicRoleAssignVO> roleAssignIPageVO = basicRoleAssignMapStruct.entity2VO(this.page(page, queryWrapper));
//        List<BasicRoleAssignEntity> basicRoleAssignList = this.list(queryWrapper);
        return roleAssignIPageVO;
    }

    @Override
    public IPage<BasicRoleAssignVO> listEnable(BasicRoleAssignParam basicRoleAssignParam) {
        LambdaQueryWrapper<BasicRoleAssignEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotNull(basicRoleAssignParam)) {
            // 根据id进行查询
            if (ObjectUtil.isNotNull(basicRoleAssignParam.getId())) {
                queryWrapper.eq(BasicRoleAssignEntity::getId, basicRoleAssignParam.getId());
            }
            if (ObjectUtil.isNotNull(basicRoleAssignParam.getFkBasicRoleId())) {
                queryWrapper.eq(BasicRoleAssignEntity::getFkRoleAssignId, basicRoleAssignParam.getFkBasicRoleId());
            }

        }
        queryWrapper.eq(BasicRoleAssignEntity::getFEnableState, CommonStatusEnum.ENABLE.getCode());
        // 根据插入时间进行升序排序
        queryWrapper.orderByAsc(BasicRoleAssignEntity::getFInsertTimeMillis);

        Page<BasicRoleAssignEntity> page = PageFactory.defaultPage(basicRoleAssignParam.getPageSize(), basicRoleAssignParam.getCurrPage());
        IPage<BasicRoleAssignVO> roleAssignIPageVO = basicRoleAssignMapStruct.entity2VO(this.page(page, queryWrapper));
//        List<BasicRoleAssignEntity> basicRoleAssignList = this.list(queryWrapper);
        return roleAssignIPageVO;
    }

    @Override
    public void add(BasicRoleAssignParam basicRoleAssignParam) {

        String roleId = basicRoleAssignParam.getFkBasicRoleId();
        getAssignRoleEntityListIdById(roleId);
        BasicRoleEntity basicRoleEntity = this.getRoleById(roleId);

        ArrayList<String> roleAssignIds = basicRoleAssignParam.getFkRoleAssignIds();
        if (CollectionUtil.isNotEmpty(roleAssignIds)) {
            for (String roleAssignId : roleAssignIds) {
//                basicRoleAssignEntity.setId("");
                BasicRoleAssignEntity basicRoleAssignEntity = new BasicRoleAssignEntity();
                basicRoleAssignEntity.setFkRoleAssignId(roleAssignId);
                basicRoleAssignEntity.setFkBasicRoleId(roleId);
                //设置启用状态
                basicRoleAssignEntity.setFEnableState(CommonStatusEnum.ENABLE.getCode());
                basicRoleAssignEntity.setId(IdUtils.getId());
                basicRoleServiceFacede.save(basicRoleAssignEntity);
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Collection<String> ids) {
        for (String id : ids) {
            BasicRoleAssignEntity basicRoleAssignEntity = this.basicRoleAssignMapper.selectById(id);
            if (ObjectUtil.isNull(basicRoleAssignEntity)) {
                throw new ServiceException(CommonExceptionEnum.DATA_NOT_EXIST.getMessage());

            }
            if (ObjectUtil.equals(CommonConstant.CAN_DELETE, basicRoleAssignEntity.getFCanDelete())) {
                this.removeById(id);
            } else {
                throw new ServiceException(DeleteUpdateExceptionEnums.CAN_NOT_DELETE.getMessage());
            }
        }
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(BasicRoleAssignParam basicRoleAssignParam) {
        // 参数校验
        checkParam(basicRoleAssignParam, true);
        //修改角色可分配角色旧数据
        BasicRoleAssignEntity basicRoleAssignEntity = this.basicRoleAssignMapper.selectById(basicRoleAssignParam.getId());
        if (ObjectUtil.isNull(basicRoleAssignEntity)) {
            throw new ServiceException(CommonExceptionEnum.DATA_NOT_EXIST.getMessage());

        }
//        BasicRoleAssignEntity currentRoleAssign = this.queryRoleAssign(basicRoleAssignParam);
        if (ObjectUtil.equals(CommonConstant.CAN_UPDATE, basicRoleAssignEntity.getFCanUpdate())) {
            BeanUtil.copyProperties(basicRoleAssignParam, basicRoleAssignEntity);
            basicRoleAssignMapper.updateById(basicRoleAssignEntity);
        } else {
            throw new ServiceException(DeleteUpdateExceptionEnums.CAN_NOT_UPDATE.getMessage());
        }
    }

    /**
     * 根据fkBasicRoleId获取该角色id已分配的角色 并删除其所有的角色id
     *
     * @param fkBasicRoleId
     */
    public void getAssignRoleEntityListIdById(String fkBasicRoleId) {
        LambdaQueryWrapper<BasicRoleAssignEntity> queryWrapper = new LambdaQueryWrapper<BasicRoleAssignEntity>()
                .eq(BasicRoleAssignEntity::getFkBasicRoleId, fkBasicRoleId);
        List<BasicRoleAssignEntity> roleAssignIdEntityList = this.list(queryWrapper);
        List<String> roleAssignVOList = new ArrayList<>();
        for (BasicRoleAssignEntity roleAssignId : roleAssignIdEntityList) {
            roleAssignVOList.add(roleAssignId.getId());
        }
        this.removeByIds(roleAssignVOList);
    }

    /**
     * 根据fkBasicRoleId获取已分配的角色id集合
     *
     * @param fkBasicRoleId
     * @return
     */
    @Override
    public List<String> getAssignRoleIdListByRoleId(String fkBasicRoleId) {
        LambdaQueryWrapper<BasicRoleAssignEntity> queryWrapper = new LambdaQueryWrapper<BasicRoleAssignEntity>()
                .eq(BasicRoleAssignEntity::getFkBasicRoleId, fkBasicRoleId);
        List<BasicRoleAssignEntity> roleAssignIdEntityList = this.list(queryWrapper);
        List<String> roleAssignVOList = new ArrayList<>();
        for (BasicRoleAssignEntity roleAssignId : roleAssignIdEntityList) {
            roleAssignVOList.add(String.valueOf(roleAssignId.getFkRoleAssignId()));
        }
        return roleAssignVOList;
    }

    /**
     * 参数校验
     *
     * @param basicRoleAssignParam
     */
    private void checkParam(BasicRoleAssignParam basicRoleAssignParam, boolean isExcludeSelf) {
        String id = basicRoleAssignParam.getId();
        LambdaQueryWrapper<BasicRoleAssignEntity> queryWrapperById = new LambdaQueryWrapper<>();
        queryWrapperById.eq(BasicRoleAssignEntity::getId, id).ne(BasicRoleAssignEntity::getFEnableState, CommonStatusEnum.DISABLE.getCode());
        if (isExcludeSelf) {
            queryWrapperById.ne(BasicRoleAssignEntity::getId, id);
        }

    }

    /**
     * 获取角色可分配角色数据
     *
     * @param basicRoleAssignParam
     * @return
     */
    private BasicRoleAssignEntity queryRoleAssign(BasicRoleAssignParam basicRoleAssignParam) {
        BasicRoleAssignEntity basicRoleAssignEntity = this.getById(basicRoleAssignParam.getId());
        if (ObjectUtil.isNull(basicRoleAssignEntity)) {
            throw new ServiceException(CommonExceptionEnum.DATA_NOT_EXIST.getMessage());
        }
        return basicRoleAssignEntity;
    }

    /**
     * 通过角色id获取角色数据
     *
     * @param id
     * @return
     */
    private BasicRoleEntity getRoleById(String id) {
        BasicRoleEntity basicRoleEntity = basicRoleMapper.selectOne(new LambdaQueryWrapper<BasicRoleEntity>().
                eq(BasicRoleEntity::getId, id));
        if (ObjectUtil.isNull(basicRoleEntity)) {
            throw new ServiceException(CommonExceptionEnum.DATA_NOT_EXIST.getMessage());
        }
        return basicRoleEntity;
    }
}
